package com.stone.persistent.recyclerview.library;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.OverScroller;

import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Field;


public class BaseRecyclerView extends RecyclerView {

    private OverScroller overScroller;
    private Object scrollerYObj;
    private Field velocityYField;

    public int getVelocityY() {
        try {
            Object var10000 = velocityYField.get(scrollerYObj);
            return (int)(float)var10000 ;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public void stopFling() {
        this.overScroller.forceFinished(true);
        try {
            this.velocityYField.set(this.scrollerYObj, 0);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public BaseRecyclerView(@NotNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Field viewFlingField = null;
        try {
            viewFlingField = RecyclerView.class.getDeclaredField("mViewFlinger");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        viewFlingField.setAccessible(true);


        Object viewFlingObj = null;
        try {
            viewFlingObj = viewFlingField.get(this);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        Field overScrollerFiled = null;
        try {
            overScrollerFiled = viewFlingObj.getClass().getDeclaredField("mOverScroller");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        overScrollerFiled.setAccessible(true);

        Object var10001 = null;
        try {
            var10001 = overScrollerFiled.get(viewFlingObj);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        this.overScroller = (OverScroller) var10001;

        Field scrollerYFiled = null;
        try {
            scrollerYFiled = OverScroller.class.getDeclaredField("mScrollerY");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        scrollerYFiled.setAccessible(true);
        try {
            var10001 = scrollerYFiled.get(this.overScroller);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        this.scrollerYObj = var10001;
        Field var8 = null;
        try {
            var8 = this.scrollerYObj.getClass().getDeclaredField("mCurrVelocity");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        this.velocityYField = var8;
        this.velocityYField.setAccessible(true);
    }


    public BaseRecyclerView(@NotNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseRecyclerView(@NotNull Context context) {
        this(context, (AttributeSet) null);
    }
}




